home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / szlibc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.8 KB  |  132 lines

  1. /* Copyright (C) 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: szlibc.c,v 1.2 2000/09/19 19:00:51 lpd Exp $ */
  20. /* Code common to zlib encoding and decoding streams */
  21. #include "std.h"
  22. #include "gserror.h"
  23. #include "gserrors.h"
  24. #include "gstypes.h"
  25. #include "gsmemory.h"
  26. #include "gsmalloc.h"
  27. #include "gsstruct.h"
  28. #include "strimpl.h"
  29. #include "szlibxx.h"
  30. #include "zconf.h"
  31.  
  32. private_st_zlib_block();
  33. private_st_zlib_dynamic_state();
  34. public_st_zlib_state();
  35.  
  36. /* Set defaults for stream parameters. */
  37. void
  38. s_zlib_set_defaults(stream_state * st)
  39. {
  40.     stream_zlib_state *const ss = (stream_zlib_state *)st;
  41.  
  42.     ss->windowBits = MAX_WBITS;
  43.     ss->no_wrapper = false;
  44.     ss->level = Z_DEFAULT_COMPRESSION;
  45.     ss->method = Z_DEFLATED;
  46.     /* DEF_MEM_LEVEL should be in zlib.h or zconf.h, but it isn't. */
  47.     ss->memLevel = min(MAX_MEM_LEVEL, 8);
  48.     ss->strategy = Z_DEFAULT_STRATEGY;
  49.     /* Clear pointers */
  50.     ss->dynamic = 0;
  51. }
  52.  
  53. /* Allocate the dynamic state. */
  54. int
  55. s_zlib_alloc_dynamic_state(stream_zlib_state *ss)
  56. {
  57.     gs_memory_t *mem = (ss->memory ? ss->memory : &gs_memory_default);
  58.     zlib_dynamic_state_t *zds =
  59.     gs_alloc_struct_immovable(mem, zlib_dynamic_state_t,
  60.                   &st_zlib_dynamic_state,
  61.                   "s_zlib_alloc_dynamic_state");
  62.  
  63.     ss->dynamic = zds;
  64.     if (zds == 0)
  65.     return_error(gs_error_VMerror);
  66.     zds->blocks = 0;
  67.     zds->memory = mem;
  68.     zds->zstate.zalloc = (alloc_func)s_zlib_alloc;
  69.     zds->zstate.zfree = (free_func)s_zlib_free;
  70.     zds->zstate.opaque = (voidpf)zds;
  71.     return 0;
  72. }
  73.  
  74. /* Free the dynamic state. */
  75. void
  76. s_zlib_free_dynamic_state(stream_zlib_state *ss)
  77. {
  78.     if (ss->dynamic)
  79.     gs_free_object(ss->dynamic->memory, ss->dynamic,
  80.                "s_zlib_free_dynamic_state");
  81. }
  82.  
  83. /* Provide zlib-compatible allocation and freeing functions. */
  84. void *
  85. s_zlib_alloc(void *zmem, uint items, uint size)
  86. {
  87.     zlib_dynamic_state_t *const zds = zmem;
  88.     gs_memory_t *mem = zds->memory;
  89.     zlib_block_t *block =
  90.     gs_alloc_struct(mem, zlib_block_t, &st_zlib_block,
  91.             "s_zlib_alloc(block)");
  92.     void *data =
  93.     gs_alloc_byte_array_immovable(mem, items, size, "s_zlib_alloc(data)");
  94.  
  95.     if (block == 0 || data == 0) {
  96.     gs_free_object(mem, data, "s_zlib_alloc(data)");
  97.     gs_free_object(mem, block, "s_zlib_alloc(block)");
  98.     return Z_NULL;
  99.     }
  100.     block->data = data;
  101.     block->next = zds->blocks;
  102.     block->prev = 0;
  103.     if (zds->blocks)
  104.     zds->blocks->prev = block;
  105.     zds->blocks = block;
  106.     return data;
  107. }
  108. void
  109. s_zlib_free(void *zmem, void *data)
  110. {
  111.     zlib_dynamic_state_t *const zds = zmem;
  112.     gs_memory_t *mem = zds->memory;
  113.     zlib_block_t *block = zds->blocks;
  114.  
  115.     gs_free_object(mem, data, "s_zlib_free(data)");
  116.     for (; ; block = block->next) {
  117.     if (block == 0) {
  118.         lprintf1("Freeing unrecorded data 0x%lx!\n", (ulong)data);
  119.         return;
  120.     }
  121.     if (block->data == data)
  122.         break;
  123.     }
  124.     if (block->next)
  125.     block->next->prev = block->prev;
  126.     if (block->prev)
  127.     block->prev->next = block->next;
  128.     else
  129.     zds->blocks = block->next;
  130.     gs_free_object(mem, block, "s_zlib_free(block)");
  131. }
  132.